package net.sudot.chess.business.service;


import net.sudot.chess.base.Chess;
import net.sudot.chess.plugin.BasePlugin;
import net.sudot.chess.plugin.login.LoginPlugin;
import net.sudot.chess.plugin.payment.PaymentPlugin;
import net.sudot.chess.plugin.storage.StoragePlugin;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 插件服务
 *
 * @author tangjialin on 2018-03-18.
 */
@Service
public class PluginService extends Chess {

    @Resource
    private List<PaymentPlugin> paymentPlugins = new ArrayList<>();
    @Resource
    private List<StoragePlugin> storagePlugins = new ArrayList<>();
    @Resource
    private List<LoginPlugin> loginPlugins = new ArrayList<>();
    @Resource
    private Map<String, PaymentPlugin> paymentPluginMap = new HashMap<>();
    @Resource
    private Map<String, StoragePlugin> storagePluginMap = new HashMap<>();
    @Resource
    private Map<String, LoginPlugin> loginPluginMap = new HashMap<>();

    /**
     * 获取所有的同类插件
     *
     * @param pluginType 插件类型
     * @return 返回插件
     */
    @SuppressWarnings("unchecked")
    public <Plugin extends BasePlugin> List<Plugin> getPlugins(String pluginType) {
        if (PaymentPlugin.class.getSimpleName().equals(pluginType)) {
            return (List<Plugin>) getPaymentPlugins();
        } else if (LoginPlugin.class.getSimpleName().equals(pluginType)) {
            return (List<Plugin>) getLoginPlugins();
        } else if (StoragePlugin.class.getSimpleName().equals(pluginType)) {
            return (List<Plugin>) getStoragePlugins();
        }
        return new ArrayList<>();
    }

    /**
     * 获取支付插件
     *
     * @return 支付插件
     */
    public List<PaymentPlugin> getPaymentPlugins() {
        Collections.sort(paymentPlugins);
        return paymentPlugins;
    }

    /**
     * 获取存储插件
     *
     * @return 存储插件
     */
    public List<StoragePlugin> getStoragePlugins() {
        Collections.sort(storagePlugins);
        return storagePlugins;
    }

    /**
     * 获取登录插件
     *
     * @return 登录插件
     */
    public List<LoginPlugin> getLoginPlugins() {
        Collections.sort(loginPlugins);
        return loginPlugins;
    }

    /**
     * 获取有效支付插件
     *
     * @param request request
     * @return 有效支付插件
     */
    public List<PaymentPlugin> getActivePaymentPlugins(HttpServletRequest request) {
        List<PaymentPlugin> result = new ArrayList<>();
        List<PaymentPlugin> paymentPlugins = getPaymentPlugins();
        for (PaymentPlugin plugin : paymentPlugins) {
            if (plugin.getIsEnabled()) {
                if (plugin.supports(request)) {
                    result.add(plugin);
                }
            }
        }
        return result;
    }

    /**
     * 获取有效存储插件
     *
     * @return 存储插件
     */
    public List<StoragePlugin> getActiveStoragePlugins() {
        List<StoragePlugin> result = new ArrayList<>();
        for (StoragePlugin plugin : getStoragePlugins()) {
            if (plugin.getIsEnabled()) {
                result.add(plugin);
            }
        }
        return result;
    }

    /**
     * 获取有效登录插件
     *
     * @param request request
     * @return 有效登录插件
     */
    public List<LoginPlugin> getActiveLoginPlugins(HttpServletRequest request) {
        List<LoginPlugin> result = new ArrayList<>();
        List<LoginPlugin> loginPlugins = getLoginPlugins();
        for (LoginPlugin plugin : loginPlugins) {
            if (plugin.getIsEnabled() && plugin.supports(request)) {
                result.add(plugin);
            }
        }
        return result;
    }

    /**
     * 获取支付插件
     *
     * @param id ID
     * @return 支付插件
     */
    public BasePlugin getPlugin(String id) {
        BasePlugin plugin = getPaymentPlugin(id);
        if (plugin == null) {
            plugin = getLoginPlugin(id);
        }
        if (plugin == null) {
            plugin = getStoragePlugin(id);
        }
        return plugin;
    }

    /**
     * 获取支付插件
     *
     * @param id ID
     * @return 支付插件
     */
    public PaymentPlugin getPaymentPlugin(String id) {
        return paymentPluginMap.get(id);
    }

    /**
     * 获取存储插件
     *
     * @param id ID
     * @return 存储插件
     */
    public StoragePlugin getStoragePlugin(String id) {
        return storagePluginMap.get(id);
    }

    /**
     * 获取登录插件
     *
     * @param id ID
     * @return 登录插件
     */
    public LoginPlugin getLoginPlugin(String id) {
        return loginPluginMap.get(id);
    }

}